import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;


public class string {
	public int xRow; int xCol;
	int iWait;
	
	@Test
	public void qS1() throws Exception{
		iWait = 3000;
		//Thread.sleep(iWait);
		String[][] xTC, xTS;
		String vKW, vXpath, vTestData;
		FirefoxDriver myD = new FirefoxDriver();
	
		
		String xPath = "C:/Priya/Silverback 4.3/Automation/QService.xls";
		
		//Read the entire TC and TS
		xTC = xlRead(xPath,"TestCases");
		xTS = xlRead(xPath, "TestSteps");
		
		
		System.out.println("Test Step sheet is" + xTS);
		
		//Go to each row in TC and identify which TC to execute
		
		for (int i = 1; i<xTC.length; i++) {   //Go to each row in TC
			System.out.println("TCID  is " + xTC[i][0]);
			//Thread.sleep(iWait);
			if (xTC[i][3].equalsIgnoreCase("Y")) {
				Thread.sleep(iWait);
				System.out.println("Yes, execute this");
				Thread.sleep(iWait);
			
			//go to every test step if it is y
			for (int j=1; j<xTS.length; j++){
				//Thread.sleep(iWait);
				System.out.println("TSID is" +xTS[i][0]);
				Thread.sleep(iWait);
				if (xTC[i][0].equalsIgnoreCase(xTS[j][1])){
					//Thread.sleep(iWait);
					System.out.println("KW to execute is " + xTS[j][4]);
					Thread.sleep(iWait);
					System.out.println("xpath to execute is " + xTS[j][5]);
					System.out.println("Data to execute is " + xTS[j][6]);
					
					
					// call the corresponding KW function
					vKW = xTS[j][4] ;
					vXpath = xTS[j][5];
					vTestData = xTS[j][6];
					
					if (vKW.equalsIgnoreCase("enterText")) {
						enterText(myD, vXpath, vTestData);
					}
						if (vKW.equalsIgnoreCase("navigateBrowser")){
						navigateBrowser(myD, vTestData);
						}
							if(vKW.equalsIgnoreCase("clickElement")){
							clickElement(myD, vXpath);
							}
								if (vKW.equalsIgnoreCase("verifyText")){
									System.out.println("Test is a" + verifyText(myD, vXpath, vTestData));
								}
								
							}
						}
						
					}
				}
			}
		
		
		
		
				
		// Capture o/p results
		
	
	

	
	//Read from Excel

	private void clickElement(FirefoxDriver myD, String vXpath) {
		// TODO Auto-generated method stub
		
	}





	private void navigateBrowser(FirefoxDriver myD, String vTestData) {
		// TODO Auto-generated method stub
		
	}





	private void enterText(FirefoxDriver myD, String vXpath, String vTestData) {
		// TODO Auto-generated method stub
		
	}





	public class ExcelRead {
	    public Object[][] main( String[] args) throws Exception{        
	    File excel = new File(args[0]);
	    FileInputStream fis = new FileInputStream(excel);
	    HSSFWorkbook wb = new HSSFWorkbook(fis);
	    HSSFSheet ws = wb.getSheet(args[1]);

	    int rowNum = ws.getLastRowNum() + 1;
	    int colNum = ws.getRow(0).getLastCellNum();
	    String[][] data = new String[rowNum][colNum];

	    for (int i = 0 ; i < rowNum ; i++) {
	        HSSFRow row = ws.getRow(i);
	            for (int j = 0 ; j < colNum ; j++) {
	                HSSFCell cell = row.getCell(j);
	                String value = cellToString(cell);
	                data[i][j] = value ;
	                //System.out.println("the value is " + value);
	            }
	        }
	    return data;
	    }

	public String cellToString(HSSFCell cell) {  
	    int type;
	    Object result;
	    type = cell.getCellType();

	    switch (type) {

	        case 0: // numeric value in Excel
	            result = cell.getNumericCellValue();
	            break;
	        case 1: // String Value in Excel 
	            result = cell.getStringCellValue();
	            break;
	        default:  
	            throw new RuntimeException("There is no support for this type of cell");                        
	    }

	    return result.toString();
	}

	
	public void enterText(FirefoxDriver driver, String xPath, String Data) {
		// Purpose: Enter text into a edit field
		//i/p: driver, xPath, Data
		//o/p:  Nil
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		driver.findElement(By.xpath(xPath)).clear();//some applications already have a default values in edit field, so this line is to clear the default path
		driver.findElement(By.xpath(xPath)).sendKeys(Data);
		
	}
	

	public void enterNum(FirefoxDriver driver, String xPath, CharSequence[] Data) {
		// Purpose: Enter text into a edit field
		//i/p: driver, xPath, Data
		//o/p:  Nil
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		driver.findElement(By.xpath(xPath)).clear();//some applications already have a default values in edit field, so this line is to clear the default path
		driver.findElement(By.xpath(xPath)).sendKeys(Data);
		
	}
	
	
	public String readText(FirefoxDriver driver, String xPath) {  //B'coz im going to get an o/p, I changed void to String to return the data
		// Purpose: read text from any field.
		//i/p: driver, xPath
		//o/p:  Data
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		return driver.findElement(By.xpath(xPath)).getText(); //returns whatever comes here
	}
	
	public String verifyText(FirefoxDriver driver, String xPath, String vText) {  //B'coz im going to get an o/p, I changed void to String to return the data
		// Purpose: verifies text from any field.
		//i/p: driver, xPath
		//o/p:  Data
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		if (vText.equalsIgnoreCase(driver.findElement(By.xpath(xPath)).getText())){  //returns whatever comes here
			
			return "pass"; 
			
		} else {
			return "Fail";
		}
		
		
	}
	
	
	
	public void clickElement(FirefoxDriver driver, String xPath) {  //B'coz im going to get an o/p, I changed void to String to return the data
		// Purpose: Click any element on the AUT
		//i/p: driver, xPath
		//o/p:  NIl
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		driver.findElement(By.xpath(xPath)).click(); // sometimes click may not work, so I write a code to send enter
		//driver.findElement(By.linkText(linkText)).click();
		//driver.findElement(By.xpath(xPath)).sendKeys("\n");
		
	}
		
	public void clickElementbylink(FirefoxDriver driver, String link) {  //B'coz im going to get an o/p, I changed void to String to return the data
		// Purpose: Click any element on the AUT
		//i/p: driver, xPath
		//o/p:  NIl
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		driver.findElement(By.linkText(link)).click();
		driver.findElement(By.linkText(link)).sendKeys("\n");
		
	}
		
	
		public void selectList(FirefoxDriver driver, String xPath, String Data) { 
			// Purpose: Select from a drop down list 
			//i/p: driver, xPath, Data
			//o/p:  NIl
			//CreatedBy : Priya
			//When :8/05/14
			//EditedBy : Priya
			//When : 8/5/14
			
		Select myList = new Select(driver.findElement(By.xpath(xPath)));//creating memory in myList
		myList.selectByVisibleText(Data);
		myList = null;  //clears the memory in my list
			
		}
			
			
	
		public void checkBox(FirefoxDriver driver, String xPath) {  //this has to be checked.
			// Purpose: checks a checkbox
			//i/p: driver, xPath
			//o/p:  NIl
			//CreatedBy : Priya
			//When :8/05/14
			//EditedBy : Priya
			//When : 8/5/14
			
		//Is it checked?
			if(driver.findElement(By.xpath(xPath)).getAttribute("checked").equalsIgnoreCase("checked")){
				//then do nothing
		
			} else {
			//click
			driver.findElement(By.xpath(xPath)).click();
			}		
		}

	//uncheckBox
	// selectRadio
	
	public void navigateBrowser(FirefoxDriver driver, String Data){
		// Purpose: Navigates a browser to a URL
					//i/p: driver, xPath
					//o/p:  NIl
					//CreatedBy : Priya
					//When :8/05/14
					//EditedBy : Priya
					//When : 8/5/14
		
		driver.get(Data);
		//driver.navigate().to(Data);
		
	}

	
	}
}



